/*
Copyright (C) Ariff Ehsan, Abdul Hadi Fikri

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <system.h>

/**
*Interface at644p_timer1{
*	Provides{
*		command void AT644P_TIMER1_init();                    @DRIVER_CLOCK_INIT();
*		command void AT644P_TIMER1_reg(void (*fired)(void));  @DRIVER_CLOCK_REG(x);
*	}
*	Uses{
*		signal void(*at644p_timer1_fired)();
*	}
*
*}
*
**
*Data Field:
*/	void (*at644p_timer1_fired)(void);

/**
 *Function Member:
 */void AT644P_TIMER1_defaultIsr(void);

/********************************************************************************************/
/*                                  Clients Function Call                                   */
/********************************************************************************************/
void AT644P_TIMER1_init(void) {
  at644p_timer1_fired = AT644P_TIMER1_defaultIsr;

  // enable output compare interrupt
  TIMSK1 = (1 << OCIE1A);
  
  // set timer1 mode of operation (CTC)
  TCCR1B = (1 << WGM12);

  // set compare value
  OCR1A = 8000;

  // clear counter
  TCNT1 = 0;

  // set timer1 prescale
  TCCR1B |= 1;
}

void AT644P_TIMER1_reg(void (*fired)(void)) {
  if (fired == NULL) {
	  return;
	}

  at644p_timer1_fired = fired;
}
/********************************************************************************************/
/*                                 Internal Function Call                                   */
/********************************************************************************************/
void AT644P_TIMER1_defaultIsr(void) {
}
/********************************************************************************************/
/*                              HW/SW Interrupt Function Call                               */
/********************************************************************************************/
/**
 *  Timer1 interrupt
 */
ISR(TIMER1_COMPA_vect) {
  (*at644p_timer1_fired)();
}


